package com.qinghuan.threads.chapterOne;

/**
 * @Author: dubao
 * @Date: 2022/5/18 20:53
 * @Description:
 *
 */

/**
 * 实现原理，可以看下源码熟悉流程
 */
public class Test1_11_1 {

    //(1) print函数
    static void print(String str) {
        //1.1 打印当前线程本地内存中localVariable变量的值
        System.out.println(str + ":" + localVariable.get());
        //1.2清除当前线程本地内存中的localVariable变量
//        localVariable.remove();

    }

    //（2）创建ThreadLocal变量
    static ThreadLocal<String> localVariable = new ThreadLocal<>();

    public static void main(String[] args) throws InterruptedException {
        //(3)创建线程one
        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                //3.1设置线程One中本地变量localVariable的值
                localVariable.set("threadOne local variable");
                //3.2调用打印函数
                print("threadOne");
                //3.3打印本地变量值
                System.out.println("threadOne remove after" + ":" + localVariable.get());
            }
        });

        Thread threadTwo = new Thread(new Runnable() {
            @Override
            public void run() {
                localVariable.set("threadTwo local variable");
                print("threadTwo");
                System.out.println("threadTwo remove after" + ":" + localVariable.get());
            }
        });

        //（5）启动线程
        threadOne.start();

        threadTwo.start();
    }

}
